package com.application.DataProvider;


import com.application.DTO.TestQuestionDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TestQuestionDAO{

	Context _mcontext = null;

	public TestQuestionDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from TestQuestion where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}

	public TestQuestionDTO getTestQuestion(int id)
	{
		TestQuestionDTO tqDto = new TestQuestionDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from TestQuestion where id = " + id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				tqDto.set_ID(Integer.valueOf(cur.getString(0)));
				tqDto.set_Content(cur.getString(1));
				if(cur.getInt(3) == 1)
					tqDto.set_QuestionY_N(true);
				else
					tqDto.set_QuestionY_N(false);
				tqDto.set_IDTestQuestionType(cur.getInt(2));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return tqDto;
	}

	public int getIDTestQuestionType(int id)
	{
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select IDTestQuestionType from TestQuestion where ID = " + id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
					result = cur.getInt(0);

			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","Result : " + result);
		return result;
	}
	
	//Insert du lieu vao bang Question
	public int insertTestQuestion(TestQuestionDTO tqDto) {
		Log.d("PEERDROID","insert chap");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", tqDto.get_ID());
			values.put("Content", tqDto.get_Content());
			values.put("QuestionYN", tqDto.get_QuestionY_N());
			values.put("IDTestQuestionType", tqDto.get_IDTestQuestionType());

			result = provider.execInsertCommand(db, "TestQuestion", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Insert du lieu vao bang Question
	public int insertTestQuestion1(TestQuestionDTO tqDto) {
		Log.d("PEERDROID","insert chap");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", tqDto.get_ID());
			values.put("Content", tqDto.get_Content());
			values.put("QuestionYN", "null");
			values.put("IDTestQuestionType", tqDto.get_IDTestQuestionType());

			result = provider.execInsertCommand(db, "TestQuestion", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	//Cap nhat bang Question
	public int updateQuestion(TestQuestionDTO tqDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Content", tqDto.get_Content());
			values.put("QuestionYN", tqDto.get_QuestionY_N());
			values.put("IDTestQuestionType", tqDto.get_IDTestQuestionType());

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(tqDto.get_ID())};
			result= provider.execUpdateCommand(db, "TestQuestion", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}

		return result;
	}

	//Delete doi tuong Question
	public int deleteTestQuestion(int id)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(id)};	
			result=provider.execDeleteCommand(db, "TestQuestion", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}
}
